<template>
  <div class="login_container">
    <div class="login_title">
      <img src="../assets/images/title.png" alt="">
      <p>智慧工地管理系统</p>
    </div>
    <div class="login_box">
      <div class="login_descriptions">
        <p class="p1">用户注册页面</p>
      </div>
      <div class="user-password">
        <img src="../assets/images/user.png" class="img1">
        <img src="../assets/images/password.png" class="img2">
      </div>

      <div class="register_form">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="registerRef">
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请确认密码" v-model="user.confirmPass"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="register">注 册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已经有账号了？请 <span style="color: #6e77f2; cursor: pointer" @click="$router.push('/login')">登录</span></div>
          </div>
        </el-form>
      </div>

    </div>


  </div>
</template>

<script>

export default {
  name: "Register",
  data() {
    // 验证码校验
    const validatePassword = (rule, confirmPass, callback) => {
      if (confirmPass === '') {
        callback(new Error('请确认密码'))
      } else if (confirmPass !== this.user.password) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      user: {
        username: '',
        password: '',
        confirmPass: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        confirmPass: [
          { validator: validatePassword, trigger: 'blur' }
        ],
      }
    }
  },
  created() {

  },
  methods: {
    register() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          console.log(valid);
          this.$axios.post(this.$httpUrl+'/register', this.user).then(res => {
            console.log(res);
            if (res.data.code == 200) {
              this.$router.push('/login')
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.login_container {
  background-image: url('@/assets/images/bcg.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
  position: absolute;
  width: 1370px;
  height: 800px;
  padding: 0px;
  top: 0;
  left: 0;
}
.login_title{
  width: 479px;
  height: 50px;
  font-size:40px;
  color: aqua;
  position: relative;
  left: 35%;
  top: 15%;
}
.login_title img{
  float: left;
  width: 50px;
  height: auto;
}
.login_title p{
  width: 420px;
  height: 45px;
  text-align: center;
}
.login_box {
  width: 479px;
  height: 521px;
  background: rgb(255, 255, 255);
  z-index: 0;
  border-radius: 3px;
  box-shadow: 0 0 10px ;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.login_descriptions{
  width: 479px;
  height: 50px;
  color: aqua;
  position: relative;
  top: 5%;
}
.p1{
  font-size: 30px;
  position: relative;
  left: 5%;
  display: inline;
}
.user-password{
  float: left;
  width: 50px;
  height: 200px;
  position: relative;
  top: 20%;
  left: 5%;
}
.img1{
  width: 50px;
  height: 50px;
  margin-bottom: 30px;
}
.img2{
  width: 50px;
  height: 50px;
}
.register_form {
  float: left;
  width: 300px;
  height: 350px;
  position: relative;
  top: 20%;
  left: 10%;
}
.input1{
  width: 300px;
  height: 50px;
  border-radius: 40% ;
  margin-bottom: 20px;
}
</style>